package com.monitor.service.impl;

import com.alibaba.fastjson.JSON;
import com.monitor.common.OkHttpUtil;
import com.monitor.common.response.CodeMsg;
import com.monitor.common.response.ResponseFactory;
import com.monitor.common.response.Result;
import com.monitor.dao.ChannelReqLogMapper;
import com.monitor.pojo.ChannelReqLog;
import com.monitor.pojo.SysUser;
import com.monitor.service.GreenChannelService;
import com.monitor.vo.GreenChannelReqVo;
import com.monitor.vo.GreenChannelRetVo;
import com.monitor.vo.enumeration.EnumQueryType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName GreenChannelServiceImpl
 * @Package com.monitor.service.impl
 * @Description TODO
 * @Author zhixingbai
 * @Create 2018/10/1417:11
 */
@Service
public class GreenChannelServiceImpl implements GreenChannelService {
    private final Logger logger=LoggerFactory.getLogger(GreenChannelServiceImpl.class);

    @Autowired
    private ChannelReqLogMapper channelReqLogMapper;

    private final Map<String,Long> reqTimeMap=new HashMap<>();

    private static final String TIME_KEY="two_query_key";

    @Override
    public Result toSendGrennChannel(GreenChannelReqVo greenChannelVo, SysUser sysUser){
        //判断两次请求时间不能超过多少秒
        Long lastReqTime=reqTimeMap.get(TIME_KEY);
        if(lastReqTime!=null){
            Long intervalTime=(System.currentTimeMillis()-lastReqTime)/1000;
            if(intervalTime<=sysUser.getIntervalTime()){
                return ResponseFactory.build(CodeMsg.TWO_QUERY_TIME,
                        String.format(CodeMsg.getMsg(CodeMsg.TWO_QUERY_TIME),
                                sysUser.getIntervalTime()));
            }
        }
        reqTimeMap.put(TIME_KEY,System.currentTimeMillis());
        String channelReqUrl;
        if(EnumQueryType.HISTORY.getCode().equals(greenChannelVo.getQueryType())){
            channelReqUrl=sysUser.getApiHisUrl();
        }else{
            channelReqUrl=sysUser.getApiNowUrl();
        }
        greenChannelVo.setUsername(sysUser.getApiUser());
        greenChannelVo.setPassword(sysUser.getApiPass());
        String reqJson=JSON.toJSONString(greenChannelVo);

        //记录库
        ChannelReqLog reqLog=new ChannelReqLog();
        reqLog.setReqTime(new Date());
        reqLog.setReqJson(reqJson);
        reqLog.setCreateTime(new Date());
        reqLog.setReqUrl(channelReqUrl);
        channelReqLogMapper.insertSelective(reqLog);
        String retJson="";
        try {
            retJson = OkHttpUtil.post(channelReqUrl, JSON.toJSONString(greenChannelVo));
            logger.info("返回结果：{}",JSON.toJSONString(retJson));
            return JSON.parseObject(retJson,GreenChannelRetVo.class);
        }catch (Exception e){
            retJson="服务器异常:"+e.getMessage();
            return ResponseFactory.build(CodeMsg.ERROR,String.format(CodeMsg.getMsg(CodeMsg.ERROR),e.getMessage()));
        }finally {
            reqLog.setRetJson(retJson);
            reqLog.setRetTime(new Date());
            reqLog.setCostTime(reqLog.getRetTime().getTime()-reqLog.getReqTime().getTime());
            channelReqLogMapper.updateByPrimaryKeySelective(reqLog);
        }
    }
}
